package com.poh.controller;

import com.poh.pojo.*;
import com.poh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Controller
public class QuestionBankController {
    @Autowired
    HomeworkService homeworkService;
    @Autowired
    QuestionBankService questionBankService;
    @Autowired
    ChoiceQuestionService choiceQuestionService;
    @Autowired
    AnswerQuestionService answerQuestionService;
    @Autowired
    CourseService courseService;

    @GetMapping("/getHomework/{courseId}")
    public String getHomework(Model model, @PathVariable("courseId") int courseId, HttpSession session) {
        Course course = courseService.getCourseByCourseId(courseId);
        session.setAttribute("course", course);
        List<QuestionBank> questionBanks = questionBankService.queryQuestionBankByCourseId(courseId);
        model.addAttribute("questionBanks", questionBanks);
        List<Homework> homeworks = homeworkService.queryHomeworkByCourseIdWithoutScore(courseId);
        model.addAttribute("homeworks", homeworks);
        return "/homework/teacherHomework";
    }

    @GetMapping("deleteQuestionBank/{questionBankId}")
    public String deleteQuestionBank(@PathVariable("questionBankId") String questionBankId, HttpSession session) {
        questionBankService.deleteQuestionBank(questionBankId);
        Course course = (Course) session.getAttribute("course");
        return "redirect:/getHomework/" + course.getCourseId();
    }

    @GetMapping("/toAddQuestionBank")
    public String toAddHomework(Model model, QuestionBank questionBank, HttpSession session) {
        QuestionBank existQuestionBank = (QuestionBank) session.getAttribute("questionBank");
        String questionBankId = null;
        if (existQuestionBank == null) {
            questionBankId = UUID.randomUUID().toString();
            Course course = (Course) session.getAttribute("course");
            session.setAttribute("questionBankId", questionBankId);

            LocalDateTime localDateTime = LocalDateTime.now();
            String questionBankName = "新建作业" + localDateTime;
            session.setAttribute("questionBankName", questionBankName);

            questionBank.setQuestionBankId(questionBankId);
            questionBank.setQuestionBankName(questionBankName);
            questionBank.setCourseId(course.getCourseId());

            questionBank.setQuestionBankCreater(course.getCourseTeacherName());

            session.setAttribute("questionBank", questionBank);
            questionBankService.addQuestionBank(questionBank);
        } else {
            QuestionBank bank = (QuestionBank) session.getAttribute("questionBank");
            List<ChoiceQuestion> choiceQuestions = choiceQuestionService.getChoiceQuestionByQuestionBankId(bank.getQuestionBankId());
            List<AnswerQuestion> answerQuestions = answerQuestionService.queryAnswerQuestionByQuestionBankId(bank.getQuestionBankId());
            if (!choiceQuestions.isEmpty()) {
                model.addAttribute("choiceQuestions", choiceQuestions);
            } else {
                model.addAttribute("choiceQuestions", null);
            }
            if (!answerQuestions.isEmpty()) {
                model.addAttribute("answerQuestions", answerQuestions);
            } else {
                model.addAttribute("answerQuestions", null);
            }
        }
        return "homework/addQuestionBank";
    }

    @PostMapping("/addQuestionBank")
    public String addHomework(QuestionBank questionBank, HttpSession session, Model model) {
        Course course = (Course) session.getAttribute("course");
        String questionBankId = (String) session.getAttribute("questionBankId");
        questionBank.setQuestionBankId(questionBankId);
        questionBankService.updateQuestionBank(questionBank);
        session.removeAttribute("questionBank");
        session.removeAttribute("questionBankId");
        session.removeAttribute("questionBankName");
        return "redirect:/getHomework/" + course.getCourseId();
    }





    @GetMapping("/intoHomework/{questionBankId}")
    public String intoTeacherHomework(@PathVariable("questionBankId")String questionBankId, Model model) {
        QuestionBank questionBank = questionBankService.queryQuestionBankById(questionBankId);
        List<ChoiceQuestion> choiceQuestions = choiceQuestionService.getChoiceQuestionByQuestionBankId(questionBankId);
        List<AnswerQuestion> answerQuestions = answerQuestionService.queryAnswerQuestionByQuestionBankId(questionBankId);
        if (!choiceQuestions.isEmpty()) {
            model.addAttribute("choiceQuestions", choiceQuestions);
        }
        if (!answerQuestions.isEmpty()) {
            model.addAttribute("answerQuestions", answerQuestions);
        }
        model.addAttribute("questionBank", questionBank);
        return "homework/teacherHomeworkInfo";
    }
}
